package com.ll.admin.service.impl;

import com.ll.admin.dao.ActionMapper;
import com.ll.admin.dao.MemberMapper;
import com.ll.admin.dao.RoleMapper;
import com.ll.admin.service.IMemberService;
import com.ll.admin.vo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class MemberServiceImpl implements IMemberService {
    @Resource
    private MemberMapper memberMapper ;
    @Resource
    private ActionMapper actionMapper ;
    @Resource
    private RoleMapper roleMapper ;

    @Override
    public Member loginUserName(String username) {
        if(username.matches("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$")) {
            return memberMapper.findByEmailReturnPassword(username) ;
        } else {
            return memberMapper.findByNameReturnPassword(username) ;
        }
    }

    @Override
    public Map<String, Set<String>> findPrivilegeByMember(String mid) {
        Map<String, Set<String>> map = new HashMap<>() ;
        map.put("allRoles",roleMapper.findRoleByMember(mid)) ;
        map.put("allActions",actionMapper.findActionByMember(mid)) ;
        return map;
    }

    @Override
    public boolean doEditPassword(String mid , String newPassword) {
        Map<String, String> map = new HashMap<>() ;
        map.put("mid",mid) ;
        map.put("password",newPassword) ;
        return memberMapper.doEditPassword(map);
    }
}
